var storeControl = {
	loadList: function(el,winName,listName,param){
		el.attr('data-parentareaid', param.ParentAreaID);
		el.attr('data-proid', param.ProID);
		el.attr('data-cityid', param.CityID);
		el.attr('data-stockid', param.StockID);
		
		$('.' + winName + ' .storeList').empty();
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2('DwShop','GetShopListByConditions',param,function(data){
			layer.closeAll('dialog');
			if(data.Success == false){
				swal(data.Message,'','error');
			}else{
				html = '';
				data.map(function(item,i){
					html += '<div data-shopcode="'+ item.ShopCode +'" data-shopname="'+ item.ShopName +'" data-shopid="'+ item.ShopID +'" class="item" '+
					'style="display:inline-block; width:49%; margin-left:1%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor:pointer;">'+ item.ShopCode + '　' + item.ShopName +'</div>'
				});
				$('.' + winName + ' .storeList').append(html);
				
				$('.storeWindow').hide();
				$('.' + winName).show();
				$('#storeShade').show();
			}
		});
	},
	//弹出框
	//el是绑定事件的对象,例如：$('xxx')
	//winName是弹窗名字（自己定义字符串，防止同一个页面多个弹窗重名）
	//listName是需要保存的列表
	openWindow: function(el,winName,listName,param,getList){
		//处理弹窗元素
		if($('#storeShade').size() == 0){
			$('body').append('<div id="storeShade" style="position:fixed; width:100%; height:100%; top:0; left:0; background: rgba(0,0,0,0.5); z-index: 10; display:none;"></div>');			
		}
		if($('.'+ winName).size() == 0){
			$('#storeShade').append(
				'<div class="storeWindow '+ winName +'" style="position:absolute; width:500px; height:300px; top:calc(50% - 150px); left:calc(50% - 250px); background:#fff; border-radius: 5px;">' +
					'<div class="sw-header" style="text-align:right; padding: 5px 10px; overflow: hidden;"><span class="close" style="font-size: 21px; color: #000; text-shadow: 0 1px 0 #fff;">×</span></div>' +
					'<div style="overflow: hidden;">' +
						'<input type="text" class="form-control input-w240 storeNameSearch" style="margin-left:10px; float:left;" placeholder="在此输入店铺名称" />' +
						'<button type="button" class="btn btn-primary findStore" style="float:left; margin-left:10px;">查找</button>' +
						'<button type="button" class="btn btn-success checkAllStore" style="float:left; margin-left:10px;">全选</button>' +
						'<button type="button" class="btn btn-info sure" style="float:left; margin-left:10px;">确定</button>' +
					'</div>' + 
					'<div class="storeList" style="margin: 5px auto 0 auto; overflow-y: auto; height: 220px;"></div>' +
				'</div>'
			)
			//关闭
			$('.' + winName + ' .close').click(function(){
				$('#storeShade').hide();
			})
			//查找
			$('.' + winName + ' .findStore').click(function(){
				var temp = $('.' + winName + ' .storeNameSearch').val().trim();
				$('.' + winName + ' .item').each(function(){
					var name = $(this).attr('data-shopname').trim();
					if(name.indexOf(temp) > -1)
						$(this).show();
					else
						$(this).hide();
				})
			})
			//单选
			$('.' + winName).on('click', '.item',function(){
				if($(this).hasClass('check'))
					$(this).removeClass('check');
				else
					$(this).addClass('check');
			})
			
			//全选
			$('.' + winName + ' .checkAllStore').click(function(){
				if($('.' + winName + ' .item:visible').size() == $('.' + winName + ' .item.check:visible').size())
					$('.' + winName + ' .item:visible').removeClass('check');
				else
					$('.' + winName + ' .item:visible').addClass('check');
			})
			//确定
			$('.' + winName + ' .sure').click(function(){
				var tempList = [];
				$('.' + winName + ' .item.check').each(function(){
					tempList.push({
						shopcode: $(this).attr('data-shopcode'),
						shopid: $(this).attr('data-shopid'),
						shopname: $(this).attr('data-shopname'),
					})
				})
				$('.' + winName + ' .close').click();
				listName = tempList;
				if(typeof(getList) == 'function')
					getList(listName);
			})
		}
		
		if(el.attr('data-parentareaid') == 'undefined' && el.attr('data-proid') == 'undefined' && el.attr('data-cityid') == 'undefined' && el.attr('data-stockid') == 'undefined'){
			this.loadList(el,winName,listName,param);
		}else{
			if(el.attr('data-parentareaid') != param.ParentAreaID || el.attr('data-proid') != param.ProID || el.attr('data-cityid') != param.CityID || el.attr('data-stockid') != param.StockID){
				this.loadList(el,winName,listName,param);
			}else{
				//初始原有数据
				$('.' + winName + ' .storeNameSearch').val('');
				$('.' + winName + ' .item').show();
				$('.' + winName + ' .item').removeClass('check');
				$('.' + winName + ' .item').each(function(){
					var temp = $(this).attr('data-shopname');
					var $this = $(this);
					listName.map(function(item){
						if(item.shopname == temp)
							$this.addClass('check');
					})
				})

				//显示相应弹窗
				$('.storeWindow').hide();
				$('.' + winName).show();
				$('#storeShade').show();
			}
		}
	},
}
